package com.wx.activitys.admin.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wx.activitys.base.utils.WebUtil;
import com.wx.activitys.drp.JsonResult;
import com.wx.activitys.drp.model.BDictionary;
import com.wx.activitys.drp.service.BDictionaryService;

@Controller
@RequestMapping("/dictionary")
public class DictionaryController {

	@Autowired
	private BDictionaryService bDictionaryService;

	/**
	 * 字典列表
	 * @param model
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model, HttpServletRequest request) throws UnsupportedEncodingException {
		// String pathRoot =
		// request.getSession().getServletContext().getRealPath("");
		getList(request, model);
		return "admin/dictionaryManage/list";
	}

	private void getList(HttpServletRequest request, Model model) throws UnsupportedEncodingException {
		int page_index = WebUtil.getInt(request, "pageNo", 1);
		int pageStartRow = (page_index - 1) * 10 ;// 每页的起始数
		
		BDictionary entity = new BDictionary();
		entity.getMap().put("pageIndex", pageStartRow);
		entity.getMap().put("pageSize", 10);
		List<BDictionary> platfromList = bDictionaryService.queryPageList(entity);
		int page_all = bDictionaryService.selectCount(entity);
		model.addAttribute("List", platfromList);
		model.addAttribute("totalSize", page_all);//总页数
		model.addAttribute("pageNo", WebUtil.getInt(request, "pageNo", 1));//当前显示页
	}

	
	
	/**
	 * 字典修改
	 * @param model
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(Model model, HttpServletRequest request) {
		BDictionary entity = new BDictionary();
		entity.getMap().put("pageIndex", 0);
		entity.getMap().put("pageSize", 10);
		List<BDictionary> list = bDictionaryService.queryPageList(entity);
		model.addAttribute("list", list);
		return "admin/dictionaryManage/form";
	}
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult save(Model model, HttpServletRequest request,BDictionary entity) throws UnsupportedEncodingException {
		int result=0;
			result = bDictionaryService.update(entity);
		if(result>0){
			return JsonResult.success("1", result);
		}else{
			return JsonResult.success("1", result);
		}
	
	}
}
